package src.com.bank;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class BankSystem {
    private Map<String, Account> accounts = new HashMap<>();
    private Scanner scanner = new Scanner(System.in);
    
    public static void main(String[] args) {
        BankSystem system = new BankSystem();
        system.start();
    }
    
    public void start() {
        System.out.println("===== 欢迎使用银行账户系统 =====");
        showHelp();
        
        boolean running = true;
        while (running) {
            System.out.print("\n请输入命令(1-7): ");
            int command = 0;
            try {
                command = scanner.nextInt();
                scanner.nextLine(); // 消耗换行符
            } catch (Exception e) {
                System.out.println("输入无效，请输入数字1-7");
                scanner.nextLine(); // 消耗无效输入
                continue;
            }
            
            switch (command) {
                case 1: createAccount(); break;
                case 2: deposit(); break;
                case 3: withdraw(); break;
                case 4: transfer(); break;
                case 5: checkBalance(); break;
                case 6: running = false; break;
                case 7: showHelp(); break;
                default: System.out.println("命令无效，请输入数字1-7");
            }
        }
        
        System.out.println("感谢使用银行账户系统，再见！");
        scanner.close();
    }
    
    private void createAccount() {
        System.out.print("请输入账户ID: ");
        String id = scanner.nextLine();
        
        if (accounts.containsKey(id)) {
            System.out.println("账户ID已存在，请选择其他ID");
            return;
        }
        
        System.out.print("请输入账户名称: ");
        String name = scanner.nextLine();
        
        System.out.print("请输入初始余额: ");
        double balance = 0;
        try {
            balance = scanner.nextDouble();
            scanner.nextLine(); // 消耗换行符
            
            Account account = new Account(id, name, balance);
            accounts.put(id, account);
            System.out.println("账户创建成功！账户信息: " + account);
        } catch (IllegalArgumentException e) {
            System.out.println("创建账户失败: " + e.getMessage());
            scanner.nextLine(); // 消耗无效输入
        } catch (Exception e) {
            System.out.println("输入无效，请输入有效的数字");
            scanner.nextLine(); // 消耗无效输入
        }
    }
    
    private void deposit() {
        Account account = getAccountById();
        if (account == null) return;
        
        System.out.print("请输入存款金额: ");
        try {
            double amount = scanner.nextDouble();
            scanner.nextLine(); // 消耗换行符
            
            account.deposit(amount);
            System.out.println("存款成功！当前余额: " + account.getBalance());
        } catch (IllegalArgumentException e) {
            System.out.println("存款失败: " + e.getMessage());
            scanner.nextLine(); // 消耗无效输入
        } catch (Exception e) {
            System.out.println("输入无效，请输入有效的数字");
            scanner.nextLine(); // 消耗无效输入
        }
    }
    
    private void withdraw() {
        Account account = getAccountById();
        if (account == null) return;
        
        System.out.print("请输入取款金额: ");
        try {
            double amount = scanner.nextDouble();
            scanner.nextLine(); // 消耗换行符
            
            account.withdraw(amount);
            System.out.println("取款成功！当前余额: " + account.getBalance());
        } catch (IllegalArgumentException e) {
            System.out.println("取款失败: " + e.getMessage());
            scanner.nextLine(); // 消耗无效输入
        } catch (InsufficientBalanceException e) {
            System.out.println("取款失败: " + e.getMessage());
            scanner.nextLine(); // 消耗无效输入
        } catch (Exception e) {
            System.out.println("输入无效，请输入有效的数字");
            scanner.nextLine(); // 消耗无效输入
        }
    }
    
    private void transfer() {
        System.out.print("请输入转出账户ID: ");
        String fromId = scanner.nextLine();
        Account fromAccount = accounts.get(fromId);
        if (fromAccount == null) {
            System.out.println("转出账户不存在");
            return;
        }
        
        System.out.print("请输入转入账户ID: ");
        String toId = scanner.nextLine();
        Account toAccount = accounts.get(toId);
        if (toAccount == null) {
            System.out.println("转入账户不存在");
            return;
        }
        
        System.out.print("请输入转账金额: ");
        try {
            double amount = scanner.nextDouble();
            scanner.nextLine(); // 消耗换行符
            
            fromAccount.transfer(toAccount, amount);
            System.out.println("转账成功！");
            System.out.println("转出账户余额: " + fromAccount.getBalance());
            System.out.println("转入账户余额: " + toAccount.getBalance());
        } catch (IllegalArgumentException e) {
            System.out.println("转账失败: " + e.getMessage());
            scanner.nextLine(); // 消耗无效输入
        } catch (InsufficientBalanceException e) {
            System.out.println("转账失败: " + e.getMessage());
            scanner.nextLine(); // 消耗无效输入
        } catch (Exception e) {
            System.out.println("输入无效，请输入有效的数字");
            scanner.nextLine(); // 消耗无效输入
        }
    }
    
    private void checkBalance() {
        Account account = getAccountById();
        if (account != null) {
            System.out.println("账户名称: " + account.getName());
            System.out.println("当前余额: " + account.getBalance());
        }
    }
    
    private void showHelp() {
        System.out.println("命令清单:");
        System.out.println("1. 创建账户");
        System.out.println("2. 存款");
        System.out.println("3. 取款");
        System.out.println("4. 转账");
        System.out.println("5. 查询余额");
        System.out.println("6. 退出系统");
        System.out.println("7. 帮助信息");
    }
    
    private Account getAccountById() {
        System.out.print("请输入账户ID: ");
        String id = scanner.nextLine();
        Account account = accounts.get(id);
        if (account == null) {
            System.out.println("账户不存在");
        }
        return account;
    }
}